<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Form{
 
	public static $binding = "form";
	public static $html = array();

	static function label($name, $value){
		if($value == false){
			return '';
		}
		$binding = Form::$binding;
		return "\t"."<label for=\"{$binding}_{$name}\">{$value}</label>"."\r\n";
	}
	
	static function wrap($html){
		return "<div>"."\r\n".$html."\r\n"."</div>"."\r\n";
	}
	
	static function attribute_string($attrs){
		$h = '';
		foreach($attrs as $name=>$value){
			$h .= "{$name}=\"{$value}\" ";
		}
		return $h;
	}
	
	static function validation_string($validations ){
		$rules = array();
		$messages = array();
		foreach($validations as $v){
			$rules[] = $v['rule'];
			$messages[] = $v['message'];
		}
		return 'data-validation-rules="'.implode(';', $rules).'" data-validation-messages="'.implode(';', $messages).'" '; 
	}
	
	static function hidden($name, $value){
		return "<input type=\"hidden\" name=\"{$name}\" value=\"{$value}\"/>";
	}
	
	static function input($name, $label, $content = '', $options=array(), $validations=array() ){
		$binding = Form::$binding;
		$opts = array(
			'type' => 'text',
			'class' => 'input-text',
			'id' => "{$binding}_{$name}",
			'name' => $name,
			'value' => $content
		);
		$options = array_merge($opts, $options);
		$html = Form::label($name, $label);
		$html .= "\t"."<input ";
		$html .= Form::attribute_string($options);
		$html .= Form::validation_string($validations);
		$html .= '/>';
		$return = Form::wrap($html);
		return $return;
	}
	
	static function textarea($name, $label, $content='', $options=array(), $validation=array()){
		$binding = Form::$binding;
		$opts = array(
			'id' => "{$binding}_{$name}"
		);
		$options = array_merge($opts, $options);
		$html = Form::label($name, $label);
		$html .= "\t"."<textarea ";
		$html .= Form::attribute_string($options);
		$html .= Form::validation_string($validation);
		$html .= ">".$content;
		$html .= "\t".'</textarea>';
		$return = Form::wrap($html);
		return $return;
	}
	
	static function select($name, $label, $contents, $selectedIndex='', $options=array(), $validation=array()){
		$binding = Form::$binding;
		$opts = array(
			'id' => "{$binding}_{$name}"
		);
		$options = array_merge($opts, $options);
		$html = Form::label($name, $label);
		$html .= '<select ';
		$html .= Form::attribute_string($options);
		if( !empty($validation) ){
			$html .= Form::validation_string($validation);
		}
		$html .= ">"."\r\n";
		$ci =& get_instance();
		$ci->load->database();
		$q = $ci->db->get($contents);
		$contents = array();
		foreach($q->result_array() as $row){
			$contents[$row['id']] = $row['name'];
		}
		foreach($contents as $id=>$content){
			$html .= "\t"."<option ";
			if($id == $selectedIndex){
				$html .= 'selected="selected" ';
			}
			$html .= "value=\"{$id}\">{$content}</option>"."\r\n";
		}
		$html .= "<select>"."\r\n";
		return Form::wrap($html);
	}
	
	static function submit($value, $options){
		$binding = Form::$binding;
		$opts = array(
			'class' => 'input-submit'
		);
		$options = array_merge($opts, $options);
		if( strpos($value, '.') ){
			$html = "<input type=\"image\" src=\"{$value}\" ";
		}else{
			$html = "<input type=\"submit\" value=\"{$value}\" ";
		}
		$html .= Form::attribute_string($options);
		$html .= '/>'."\r\n";
		return $html;
	}
}

function bound_form($bind, $url, $options=array(), $content = array() ){
	$opts = array(
		'method' => 'POST',
		'print' => true,
		'fieldset' => true,
		'legend' => '',
		'submit_value' => 'Submit'
	);
	$options = array_merge($opts, $options);
	$submit_value = $options['submit_value'];
	unset($options['submit_value']);
	extract($options);
	require BASE_PATH.'application/models/bindings/'.$bind.'.php';
	Form::$binding = $bind;
	foreach($binding as $name=>$b){
		if( !isset($b['options']) ){
			$b['options'] = array();
		}
		if( !isset($b['validation']) ){
			$b['validation'] = array();
		}
		if( isset($content[$name]) ){
			$value = $content[$name];
		}else{
			$value = '';
		}
		switch($b['type']){
			case 'select' :
				Form::$html[] = Form::select($name, $b['label'], $b['data'], $value, $b['options'], $b['validation']);
			break;
			case 'textarea':
				Form::$html[] = Form::textarea($name, $b['label'], $value, $b['options'], $b['validation']);
			break;
			default:
				$b['options']['type'] = $b['type'];
				Form::$html[] = Form::input($name, $b['label'], $value, $b['options'], $b['validation']);
			break;
		}
	}
	Form::$html[] = Form::submit($submit_value, array());
	if($print){
		bound_form_print($bind, $url, $method, $fieldset, $legend);
	}
}

function bound_form_print($name, $url, $method = 'POST', $fieldset = true, $legend = false){
	$ci =& get_instance();
	$url = $ci->config->item('base_url').$url;
	$html = "<form id=\"{$name}_form\" method=\"{$method}\" action=\"{$url}\">"."\r\n";
	if($fieldset){
		$html .= "<fieldset>"."\r\n";
	}
	if($fieldset && $legend){
		$html .= "<legend>{$legend}</legend>";
	}elseif($fieldset && $legend == ''){
		$html .= "<legend>{$name}</legend>";
	}
	foreach(Form::$html as $h){
		$html .= $h;
	}
	if($fieldset){
		$html .= "</fieldset>"."\r\n";
	}
	$html .= "</form>";
	print $html;
}

function bound_form_add($html, $index=1000){
	$before = array();
	$after = array();
	$adding = array($html);
	foreach(Form::$html as $i=>$h){
		if($i<$index){
			$before[] = $h;
		}else{
			$after[] = $h;
		}
	}
	Form::$html =  array_merge($before, $adding, $after);
}

function bound_form_remove($index){
	if( is_int($index) ){
		array_splice(Form::$html, $index, 1);	
	}
	return true;
}


?>